import {
    get,
    post,
    del,
    controller
} from "../lib/decorator";
import mongoose from 'mongoose'
const Place = mongoose.model('Place')

@controller('/api/v0/places')
export class placeController {
    @get('/')
    async index(ctx, next){
        let query = {}

        const places = await Place.find(query)

        ctx.body = {
            success: true,
            data: places
        }
    }

    @get('/insert')
    async placeInsert(ctx, next){
        let query = new Place({
            placeName: '一个神秘的地方'
        })

        let place = await Place.find(query)
        if( !place ){
            place = new Place(query)
            place = await place.save()
        } else {
            place = '已经存在了'
        }

        ctx.body = {
            success: true,
            data: place
        }
    }

    @get('/del')
    async delPlace(ctx, next){
        let places = await Place.find({})

        await Place.remove({
            _id: {
                $in: places
            }
        })

        ctx.body = {
            success: true,
            data: places
        }

    }
}